package com.ruoyi.patrol.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.patrol.mapper.PtSchoolMapper;
import com.ruoyi.patrol.domain.PtSchool;
import com.ruoyi.patrol.service.IPtSchoolService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学校信息Service业务层处理
 *
 * @author lkm
 * @date 2022-11-08
 */
@Service
public class PtSchoolServiceImpl implements IPtSchoolService {
    @Autowired
    private PtSchoolMapper ptSchoolMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    /**
     * 查询学校信息
     *
     * @param id 学校信息ID
     * @return 学校信息
     */
    @Override
    public PtSchool selectPtSchoolById(Long id) {
        return ptSchoolMapper.selectPtSchoolById(id);
    }

    /**
     * 查询学校信息列表
     *
     * @param ptSchool 学校信息
     * @return 学校信息
     */
    @Override
    public List<PtSchool> selectPtSchoolList(PtSchool ptSchool) {
        return ptSchoolMapper.selectPtSchoolList(ptSchool);
    }

    /**
     * 查询学校信息列表包含人员数量
     *
     * @param ptSchool 学校信息
     * @return 学校信息
     */
    @Override
    public List<PtSchool> selectPtSchoolStaffAndGuardNum(PtSchool ptSchool) {
        return ptSchoolMapper.selectPtSchoolStaffAndGuardNum(ptSchool);
    }

    /**
     * 新增学校信息
     *
     * @param ptSchool 学校信息
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertPtSchool(PtSchool ptSchool) {
        ptSchool.setCreateTime(DateUtils.getNowDate());
        ptSchool.setCreateBy(SecurityUtils.getUsername());
        int i = ptSchoolMapper.insertPtSchool(ptSchool);
        // 为学校添加管理员登录账号
        SysUser user = new SysUser();
        user.setUserName(ptSchool.getSysUser().getPhonenumber());
        user.setNickName(ptSchool.getSysUser().getNickName());
        user.setPhonenumber(ptSchool.getSysUser().getPhonenumber());
        // 密码默认为电话号码后6位
//        user.setPassword(SecurityUtils.encryptPassword(ptSchool.getSysUser().getPhonenumber().substring(5)));
        user.setPassword(SecurityUtils.encryptPassword("XYxc123456!@"));
        user.setCreateBy(SecurityUtils.getUsername());
        user.setSchoolId(ptSchool.getId());
        user.setSchoolAdminFlag("0");
        userService.insertUser(user);
        // 为账号添加角色
        SysRole role = roleService.selectRoleByRoleKey("schoolAdmin");
        SysUserRole userRole = new SysUserRole();
        userRole.setRoleId(role.getRoleId());
        userRole.setUserId(user.getUserId());
        userRoleMapper.insertUserRole(userRole);
        return i;
    }

    /**
     * 修改学校信息
     *
     * @param ptSchool 学校信息
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updatePtSchool(PtSchool ptSchool) {
        ptSchool.setUpdateTime(DateUtils.getNowDate());
        ptSchool.setUpdateBy(SecurityUtils.getUsername());
        int i = ptSchoolMapper.updatePtSchool(ptSchool);
        // 更新账号信息
        SysUser user = userService.selectUserBySchoolId(ptSchool.getId());
        user.setUserName(ptSchool.getSysUser().getPhonenumber());
        user.setNickName(ptSchool.getSysUser().getNickName());
        user.setPhonenumber(ptSchool.getSysUser().getPhonenumber());
        // 密码默认为电话号码后6位
//        user.setPassword(SecurityUtils.encryptPassword(ptSchool.getSysUser().getPhonenumber().substring(5)));
        user.setUpdateBy(SecurityUtils.getUsername());
        userService.updateUserMy(user);
        return i;
    }

    /**
     * 批量删除学校信息
     *
     * @param ids 需要删除的学校信息ID
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deletePtSchoolByIds(Long[] ids) {
        int i = ptSchoolMapper.deletePtSchoolByIds(ids);
        userService.deleteUserBySchoolIds(ids);
        return i;
    }

    /**
     * 删除学校信息信息
     *
     * @param id 学校信息ID
     * @return 结果
     */
    @Override
    public int deletePtSchoolById(Long id) {
        return ptSchoolMapper.deletePtSchoolById(id);
    }

    @Override
    public int selectSchoolCount() {
        return ptSchoolMapper.selectSchoolCount();
    }

    @Override
    public PtSchool selectPtSchoolByName(String name) {
        return ptSchoolMapper.selectPtSchoolByName(name);
    }

    @Override
    public PtSchool selectPtSchoolBySchoolName(String schoolName) {
        return ptSchoolMapper.selectPtSchoolBySchoolName(schoolName);
    }

}
